package com.heima.search.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.dtos.UserSearchDto;
import com.heima.model.search.pojos.ApAssociateWords;
import com.heima.search.service.ApAssociateWordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

@Service
public class ApAssociateWordsServiceImpl implements ApAssociateWordsService {
    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 联想词
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findAssociate(UserSearchDto dto) {
        if (StringUtils.isEmpty(dto.getSearchWords())) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

        if(dto.getPageSize() > 20){
            dto.setPageSize(20);
        }

        Query associateWords = Query.query(Criteria.where("associateWords").regex("^.*" + dto.getSearchWords() + ".*$"))
                .limit(dto.getPageSize());
        Query query = associateWords;
        mongoTemplate.find(query , ApAssociateWords.class);
        return ResponseResult.okResult(associateWords);
    }
}
